Jetpack WindowManager 1.1 稳定版
获取窗口指标 (和大小类别!)
WindowMetricsCalculator
https://developer.android.google.cn/reference/kotlin/androidx/window/layout/WindowMetricsCalculator
在此基础上,窗口大小类别是一组视口划分点,有助于您设计、开发和测试响应式和自适应应用布局。此类划分点专门用于平衡布局的简单性和灵活性,以便针对特殊情况优化您的应用。
窗口大小类别
https://developer.android.google.cn/guide/topics/large-screens/support-different-screen-sizes#window_size_classes
对于基于 View 的应用,您可以使用以下代码段来计算窗口大小类别:
private fun computeWindowSizeClasses() {
val metrics = WindowMetricsCalculator.getOrCreate()
.computeCurrentWindowMetrics(this)
val width = metrics.bounds.width()
val height = metrics.bounds.height()
val density = resources.displayMetrics.density
val windowSizeClass = WindowSizeClass.compute(width/density, height/density)
// use windowSizeClass.windowHeightSizeClass and windowSizeClass.windowWidthSizeClass
}
您可以参阅 "支持不同屏幕尺寸" 开发者指南了解详细信息。
支持不同屏幕尺寸
https://developer.android.google.cn/guide/topics/large-screens/support-different-screen-sizes
让应用能够感知折叠
状态: 设备的折叠状态: FLAT 或 HALF_OPENED
方向: 折叠或设备合页的方向: HORIZONTAL 或 VERTICAL 遮挡类型: 折叠或合页是否遮挡部分显示屏: NONE 或 FULL 是否分离: 折叠或合页是否创建了两个逻辑显示区域: true 或 false 边界: 应用窗口内功能的边界矩形 (继承自 DisplayFeature)
WindowInfoTracker https://developer.android.google.cn/reference/kotlin/androidx/window/layout/WindowInfoTracker FoldingFeature https://developer.android.google.cn/reference/androidx/window/layout/FoldingFeature DisplayFeature https://developer.android.google.cn/reference/androidx/window/layout/DisplayFeature#getBounds%28%29
您可以通过 Flow 访问这些数据:
override fun onCreate(savedInstanceState: Bundle?) {
...
lifecycleScope.launch(Dispatchers.Main) {
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
WindowInfoTracker.getOrCreate(this@MainActivity)
.windowLayoutInfo(this@MainActivity)
.collect { layoutInfo ->
// New posture information
val foldingFeature = layoutInfo.displayFeatures
// use the folding feature to update the layout
}
}
}
}
Flow
https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/
MediaPlayerActivity.kt
https://github.com/android/platform-samples/blob/main/samples/user-interface/windowmanager/src/main/java/com/example/platform/ui/windowmanager/MediaPlayerActivity.kt
并排显示两个 Activity
Activity 嵌入
https://developer.android.google.cn/reference/kotlin/androidx/window/embedding/package-summary
SlidingPaneLayout
https://developer.android.google.cn/jetpack/androidx/releases/slidingpanelayout
修改分屏行为 (分屏比例、规则、结束行为)
定义占位符 在运行时环境中检查 (并更改) 分屏状态 实现水平分屏 在全窗口中启动模态
https://developer.android.google.cn/codelabs/large-screens/activity-embedding
Activity 嵌入
https://developer.android.google.cn/guide/topics/large-screens/activity-embedding#cross-application
总结
1.2 版本
https://developer.android.google.cn/jetpack/androidx/releases/window#version_12_2
文档
https://developer.android.google.cn/jetpack/androidx/releases/window示例应用
https://github.com/android/platform-samples/tree/main/samples/user-interface/windowmanager
推荐阅读